<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    {{ info.age }}
    <div><span>账号</span><input v-model="userName" /></div>
    <div><span>密码</span><input v-model="userpwd" /></div>
    <div>
      <button @click="login()">登录</button>
      >
      <button @click="register()">注册</button>
    </div>
  </div>
</template>

<script>
import { inject, ref, getCurrentInstance } from "vue";

export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  setup() {
    const { proxy } = getCurrentInstance();
    let info = inject("info");
    let userName = ref("yueguiming521");
    let userpwd = ref("yueguiming521");
    const register = () => {
      proxy.$api
        .post("/register", { user_id: userName.value, pwd: userpwd.value })
        .then((res) => {
          console.log(res);
        });
    };
    const login = () => {
      proxy.$api
        .post("/login", { user_id: userName.value, pwd: userpwd.value })
        .then((res) => {
          console.log(res.data.user_id);
          info.id = res.data.user_id;
        });
    };
    return {
      info,
      userName,
      userpwd,
      login,
      register,
    };
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.hello {
  width: 1rem;
  height: 1rem;
  margin: auto;
  background: aqua;
  font-size: 0.1rem;
  overflow: hidden;
  h1 {
    font-size: 0.1rem !important;
  }
}
</style>
